/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ems.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ems.entity.EmsCart;
import com.thinkgem.jeesite.modules.ems.entity.EmsStudent;
import com.thinkgem.jeesite.modules.ems.service.EmsCartService;
import com.thinkgem.jeesite.modules.ems.service.EmsStudentService;

/**
 * 学员Controller
 * @author zad
 * @version 2018-01-25
 */
@Controller
@RequestMapping(value = "${adminPath}/ems/emsJoin")
public class EmsJoinController extends BaseController {

	@Autowired
	private EmsStudentService emsStudentService;
	@Autowired
	private EmsCartService emsCartService;
	
	@ModelAttribute
	public EmsStudent get(@RequestParam(required=false) String id) {
		EmsStudent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = emsStudentService.get(id);
		}
		if (entity == null){
			entity = new EmsStudent();
		}
		return entity;
	}
	
	@RequiresPermissions("ems:emsJoin:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmsStudent emsStudent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmsStudent> page = emsStudentService.findPage(new Page<EmsStudent>(request, response), emsStudent); 
		model.addAttribute("page", page);
		return "modules/ems/emsJoinStudentList";
	}

	/**
	 * 
	 * @param newType add:未报过名的新生；1：报过名的新生；0：老生
	 * @param emsStudent
	 * @param model
	 * @return
	 */
	@RequiresPermissions("ems:emsJoin:view")
	@RequestMapping(value = "form")
	public String form(String newType,EmsStudent emsStudent, Model model) {
		
		// 当学员为未报过名的新生时，将isNew字段置为新生
		if("add".equals(newType))
		{
			
			
			// add by zad 2018-01-30 添加时初始化学号等信息
			if(null == emsStudent || StringUtils.isBlank(emsStudent.getId()))
			{
				if(null == emsStudent)
				{
					emsStudent = new EmsStudent();
				}
				// 添加时，当学号为空才生成，否则不再生成
				if(StringUtils.isBlank(emsStudent.getNo())){
					emsStudent.setNo(this.emsStudentService.generateStuNo());
				}
			}
			
			emsStudent.setIsNew(Global.YES);
		}
		
		model.addAttribute("emsStudent", emsStudent);
		model.addAttribute("newType", newType);
		return "modules/ems/emsJoinForm";
	}


	@RequiresPermissions("ems:emsStudent:edit")
	@RequestMapping(value = "save")
	public String save(String newType,EmsCart emsCart, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, emsCart)){
			return "modules/ems/emsJoinForm";
		}
		
		// 类型为新增学员时，需要保存学员信息
		if("add".equals(newType))
		{
			this.emsStudentService.save(emsCart.getStudent());
		}
		// 类型为新学员时（学员信息表中已有信息），需要更新学员信息
		if(Global.YES.equals(newType))
		{
			emsCart.getStudent().setIsNew(Global.NO);
			this.emsStudentService.save(emsCart.getStudent());
		}
		
		// add by zad // 状态为未培训
		emsCart.setStatus(Global.NO);
		
		emsCartService.save(emsCart);
		addMessage(redirectAttributes, "保存购物车成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsJoin/?repage";
	}
	
	@RequiresPermissions("ems:emsJoin:edit")
	@RequestMapping(value = "delete")
	public String delete(EmsStudent emsStudent, RedirectAttributes redirectAttributes) {
		emsStudentService.delete(emsStudent);
		addMessage(redirectAttributes, "删除学员成功");
		return "redirect:"+Global.getAdminPath()+"/ems/emsStudent/?repage";
	}
	
	
	@RequiresPermissions("ems:emsJoin:view")
	@RequestMapping(value = {"checkExists"})
	public String checkExists(EmsStudent emsStudent, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Map<String,String> rs = new HashMap<String,String>();
		List<EmsStudent> students = this.emsStudentService.checkExists(emsStudent);
		if(null != students && students.size() > 0)
		{
			rs.put("rs", "yes");
		}
		else
		{
			rs.put("rs", "no");
		}
		
		String jsonStr = JsonMapper.toJsonString(rs);
		try {
			response.getWriter().print(jsonStr);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}